package com.hl.card.modular.packBack.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.dao.CommunityUserMapper;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.CommunityUser;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.packBack.service.CommunityUserService;

/**
 * 社区管理员业务逻辑类
 * 
 * @author 袁伟
 *
 */
@Service
public class CommunityUserServiceImpl implements CommunityUserService{
    @Autowired
    private CommunityUserMapper communityUserMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 添加
     */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void add(String user_id) {
	CommunityUser communityUser=new CommunityUser();	
	communityUser.setUser_id(user_id);	
	communityUser.setInvite_user_max_count(499);
	communityUser.setStatus(0);
	communityUser.setAdd_time(DateUtils.getNow());
	communityUserMapper.add(communityUser);
	Map<String,Object> param=new HashMap<String, Object>();
	param.put("user_id", user_id);
	param.put("status", 1);
	userMapper.updateCommunityUser(param);
	}
	
	/**
	 * 根据用户ID查找
	 */
	@Override
	public CommunityUser findByUserId(String user_id) {
		return communityUserMapper.findByUserId(user_id);
	}

	@Override
	public List<CommunityUser> list(Page<CommunityUser> page, CommunityUser user,String sort,String order) {
		int skip = (page.getCurrent() - 1) * page.getSize();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("offset", skip);
        map.put("size", page.getSize());
        if(!StringUtils.isEmpty(sort))
        {
        	map.put("orderFiled", "order by "+sort+" "+order);
        }
       if(!StringUtils.isEmpty(user.getPhone()))
        {
        	 map.put("phone", user.getPhone());
        }
        page.setTotal(communityUserMapper.listCount(map));
		return communityUserMapper.list(map);
	}

	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void switchStatus(Integer status, Integer id) {
		CommunityUser communityUser=communityUserMapper.findbyId(id);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("status", status);
		map.put("id", id);
		Map<String,Object> param=new HashMap<String, Object>();
		param.put("user_id", communityUser.getUser_id());
		if(status==0)  //启用
		{
			param.put("status", 1);
			
		}else
		{
			param.put("status", 0);
		}
		userMapper.updateCommunityUser(param);
		communityUserMapper.switchStatus(map);
		
	}

	/**
	 * 批量修改
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void updateBatch(Integer[] ids, Integer[] invite_user_max_counts,String[] community_codes) {
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		for(int i=0;i<ids.length;i++)
		{
			Map<String,Object> map=new HashMap<String, Object>();
			map.put("id", ids[i]);
			map.put("invite_user_max_count", invite_user_max_counts[i]);
			map.put("community_code", community_codes[i]);
			list.add(map);
		}
		communityUserMapper.updateBatch(list);
		
	}

	@Override
	public List<Map<String, Object>> exportExcel() {
		return communityUserMapper.exportExcel();
	}
    
}
